const util = require("../../utils/util")

const app = getApp()

Page({
  data: {
    
  },
  opendLocation(event){
    console.log(event.currentTarget.dataset.index)
    var that = this;
    wx.openLocation({
      latitude: new Number(that.data.actionsList[event.currentTarget.dataset.index].cLatitude),
      longitude: new Number(that.data.actionsList[event.currentTarget.dataset.index].cLongitude),
    })
  },
  // 跳转用户页面
  toUserDetail(e){
    console.log(e.currentTarget.dataset.openid)
    wx.navigateTo({
      url: '/pages/me/me?openid=' + e.currentTarget.dataset.openid,
    })
  },
  onShow(){
    this.getActionsList()
  },
  onLoad: function () {

    console.log(app.globalData.userInfo)

    var that = this;
    setTimeout(function(){
      console.log(app.globalData.openid)
      that.setData({
        myOpenid: app.globalData.openid
      })
    },2000)
  },
  // 获取动态
  getActionsList(){
    var that = this // desc asc
    wx.request({
      url: app.globalData.baseUrl + 'api/actions/v1/wx/select',
      method:"POST",
      data:{},
      success(res){
        console.log(res)
        var list = res.data.data
        for(var l in list){
          list[l].dtCreate = util.formatTime(new Date(list[l].dtCreate))
        }

        for(var l in list){
          for(var j in list[l].prizeList){
            // 如果点赞列表有本人信息则设置为true
            if(list[l].prizeList[j].openid == app.globalData.openid){
              list[l].isPrized = true
            }
          }
        }
        for(var l in list){
          if(list[l].commentList.length != 0){
            for(var j in list[l].commentList){
              list[l].commentList[j].time = util.formatTime(new Date(list[l].commentList[j].time))
            }

          }
        }
        that.setData({
          actionsList :list
        })
      }
    })
  },

  // 添加登录
  toPublish(){
    if(app.globalData.userInfo == null){
      wx.switchTab({
        url: '/pages/me/me',
        success(){
          wx.showToast({
            icon:'error',
            title: '请登录',
          })
        }
      })
    }else {
      wx.navigateTo({
        url: '/pages/actions/publish/publish',
      })
    }
    
  },
  // 进入说说详情页面
  toDetail(event){
    console.log(event.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/actions/detail/detail?id=' + event.currentTarget.dataset.id,
    })
  },
  // 删除说说
  deleteAction(event){
    console.log(event.currentTarget.dataset.id)
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'api/actions/v1/wx/remove',
      data:{
        cId: event.currentTarget.dataset.id
      },
      method:'POST',
      success(res){
        wx.showToast({
          title: '删除成功！',
        })
        that.getActionsList()
      }
    });
  },

  onPullDownRefresh(){
    this.getActionsList()
  },
  // 点赞
  pirzeAction(event){
    if(app.globalData.userInfo == null){
      wx.switchTab({
        url: '/pages/me/me',
        success(){
          wx.showToast({
            icon:'error',
            title: '请登录',
          })
        }
      })
    }else {
      console.log(event.currentTarget.dataset.id)
      var that = this;
      wx.request({
        url: app.globalData.baseUrl + 'api/actions/v1/wx/getById',
        method:"POST",
        data:{
          cId:event.currentTarget.dataset.id
        },
        success(rs){
          console.log(rs)
          var action = rs.data.data
          var tag = false
          var index 
          for(var l in action.prizeList){
            if(action.prizeList[l].openid == app.globalData.openid){
              tag = true
              index = l
              break
            }
          }
          if(tag){
            //之前点赞过 删除点赞记录
            //解决手机取消点赞时候设置为null的bug
            let prizeList = []
            for(let i in action.prizeList){
              if(index != i){
                prizeList.push(action.prizeList[i])
              }
            }
            console.log(action)
            wx.request({
              url: app.globalData.baseUrl + 'api/actions/v1/wx/saveOrUpdate',
              method:"POST",
              data:{
                cId: event.currentTarget.dataset.id,
                prizeList: prizeList
              },
              success(res){
                that.getActionsList();
              }
            })
          }else{
            //之前未点赞  添加点赞记录
            var user = {}
            user.nickName = app.globalData.userInfo.cNickName
            user.faceImg = app.globalData.userInfo.cIcon
            user.openid = app.globalData.openid

            action.prizeList.push(user)

            console.log(action.prizeList)
            wx.request({
              url: app.globalData.baseUrl + 'api/actions/v1/wx/saveOrUpdate',
              method:"POST",
              data:{
                cId: event.currentTarget.dataset.id,
                prizeList: action.prizeList
              },
              success(res){
              wx.showToast({
                  title: '点赞成功！',
                })
                that.getActionsList();
              }
            })
          }
        }
      })
    }
  },
  // 删除评论
  delteComment(event){
    var that = this;
    console.log(event.currentTarget.dataset.id)
    console.log(event.currentTarget.dataset.index)

    wx.showModal({
      title:'提示',
      content:'确定要删除此评论吗？',
      success(res){
        if(res.confirm){
          var index = event.currentTarget.dataset.index

          wx.request({
            url: app.globalData.baseUrl + 'api/actions/v1/wx/getById',
            method:"POST",
            data:{
              cId: event.currentTarget.dataset.id
            },
            success(res){
              var action = res.data.data;
              action.commentList.splice(index,1);
              wx.request({
                url: app.globalData.baseUrl + 'api/actions/v1/wx/saveOrUpdate',
                method:"POST",
                data:{
                  cId: event.currentTarget.dataset.id,
                  commentList: action.commentList
                },
                success(rs){
                  wx.showToast({
                    title: '删除成功',
                  })
                  that.getActionsList()
                }
              })
            }
          })
        }else if(res.cancel){

        }
      }
    })
    



  },

  // 分享
  onShareAppMessage(event){
    if(event.from == 'button'){
      console.log(event.target.dataset.index)
      var index = event.target.dataset.index
      return {
        title: this.data.actionsList[index].cText,
        imageUrl: this.data.actionsList[index].images[0],
        path:'pages/actions/detail/detail?id=' + this.data.actionsList[index].cId
      }
    }
    if(event.from == 'menu'){
      return {
        title: '欢迎进入朋友圈列表',
        imageUrl: '',
        path:'pages/index/index'
      }
    }
    

  },

  previewImg(event){
    var that = this;
    console.log(event)
    
    wx.previewImage({
      current: event.currentTarget.dataset.src,//当前显示图片的路径
      urls: that.data.actionsList[event.currentTarget.dataset.index].images,
    })


  }

})
